package com.hud.easynavi;

/**
 * Created by lucian on 17-3-13.
 */

public class CanDataThread extends Thread implements CanBusDataListener
{
    public interface CanReceiveListener{
        void onCanDataReceived(int channel, int can_id, byte[] data);
    }

    CanReceiveListener listener;

    String canInterfaceName;
    final static String TAG = "com.hud.easynavi.CanDataThread";

    public CanDataThread(String can){
        canInterfaceName = can;
    }

    public void setListener(CanReceiveListener lis){
        listener = lis;
    }

    @Override
    public void run() {
        if(canInterfaceName.equalsIgnoreCase("can0")){
            CanBusNative.startCanBus(0,this);
        }else if(canInterfaceName.equalsIgnoreCase("can1")){
            CanBusNative.startCanBus(1,this);
        }
    }

    @Override
    public void onCanBusDataReceive(int channel,int can_id, byte[] data) {
        Log(TAG,canInterfaceName+" data receive... can_id:"+Integer.toHexString(can_id));
        if(listener!=null){
            listener.onCanDataReceived(channel,can_id,data);
        }
    }

    public void stopCAN(){
        if(canInterfaceName.equalsIgnoreCase("can0")){
            CanBusNative.stopCanBus(0);
        }else if(canInterfaceName.equalsIgnoreCase("can1")){
            CanBusNative.stopCanBus(1);
        }
    }

    private void Log(String TAG,String msg){
        System.out.println(TAG+msg);
    }
}
